/*
 * Ganesa Copyright (C) 2008 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.ganesa.application.ui;

import java.io.File;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import be.gnx.ganesa.application.controller.Exit;
import be.gnx.ganesa.application.main.PluginsLoader;
import be.gnx.ganesa.application.model.Application;

public class Ganesa {

  private Application application = Application.getInstance();

  private StatusBar statusBar;

  private InfoBar infoBar;

  //  private ToolBar toolBar;

  private Map map;

  public Ganesa(Shell shell) {
    new MenuBar(this, shell);
    statusBar = new StatusBar(this, shell);
    infoBar = new InfoBar(this, shell);
    map = new Map(this, shell);

    shell.addListener(SWT.Close, new Exit(this));
    resize();
  }

  public Application getApplication() {
    return application;
  }

  public void resize() {
    statusBar.resize();
    infoBar.resize();
    map.resize(application);
  }

  public StatusBar getStatusBar() {
    return statusBar;
  }

  public InfoBar getInfoBar() {
    return infoBar;
  }

  public static void main(String[] args) {
    String userHome = System.getProperty("user.home");
    File configDirectory = new File(new File(userHome), ".ganesa");
    File pluginsDirectory = new File(configDirectory, "plugins");
    if (!pluginsDirectory.exists()) {
      pluginsDirectory.mkdirs();
    }
    Vector<File> pluginsDirectories = new Vector<File>();
    pluginsDirectories.add(pluginsDirectory);
    new PluginsLoader(pluginsDirectories);

    //Display.setAppName("Ganesa"); // not supported buy rwt/rap
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setMaximized(true);
    shell.setText("Ganesa");
    //shell.setImage(null); // this is to set an icon, does not work with SWTSwing

    final Ganesa ganesa = new Ganesa(shell);
    shell.addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent controlEvent) {
        ganesa.resize();
      }
    });

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }

}
